/*
 * Created on 25/mag/07
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package it.unibo.deis.lia.mmhc.com;

import it.unibo.deis.lia.location.*;
import it.unibo.deis.lia.networkInterface.*;
import it.unibo.deis.lia.mmhc.util.*;

import java.util.*;
import java.io.*;

public class ConnectorConfigurator {

	// bss mac address --> wep key
	private Hashtable<String, String> wepKeysDB = new Hashtable<String, String>();
	// bss mac address --> Vector [wpa username, wpa password]
	private Hashtable<String, Vector<String>> wpaPairsDB = new Hashtable<String, Vector<String>>();

	protected Vector<Channel> getSuitableChannels(
			Vector<Connector> suitableConnectors,
			Vector<Channel> suitableChannels) throws Exception {
		Vector<Channel> newSuitableChannels = new Vector<Channel>();
		for (int i = 0; i < suitableConnectors.size(); i++) {
			Connector connector = suitableConnectors.elementAt(i);

			boolean alreadyAvailable = false;
			System.out.println("\tsuitableChannels.size() "
					+ suitableChannels.size());
			for (int j = 0; j < suitableChannels.size() && !alreadyAvailable; j++) {
				Channel alreadyAvailableChannel = suitableChannels.elementAt(j);
				String tempMac = alreadyAvailableChannel.getConnector()
						.getConnectorDescription().getId();
				System.out.println("\ttempMac " + tempMac);
				System.out
						.println("\tconnector.getConnectorDescription().getId() "
								+ connector.getConnectorDescription().getId());
				if (tempMac.equals(connector.getConnectorDescription().getId())) {
					if (alreadyAvailableChannel.getChannelLocalIPAddress() != null) {
						alreadyAvailable = true;
					}
				}
			}/* verifica se il mac è già stato registrato */

			System.out.println("\talreadyAvailable " + alreadyAvailable);
			if (!alreadyAvailable) {
				if (connector.getConnectorDescription() instanceof BSS) {
					BSS bss = (BSS) connector.getConnectorDescription();

					// encryption management:
					// a) WEP ==> require WEP key
					// b) WPA ==> require username/password pair
					if (bss.getEncryption() == BSS.WEP) {
						if (wepKeysDB.contains(bss.getId())) {
							bss.setWEPKey(wepKeysDB.get(bss.getId()));
						} else {
							BufferedReader br = new BufferedReader(
									new InputStreamReader(System.in));
							System.out
									.println("Insert WEP key (ASCII, please):");
							String newWEPKey = br.readLine();
							if (!newWEPKey.equals("")) {
								bss.setWEPKey(newWEPKey);
								wepKeysDB.put(bss.getId(), newWEPKey);
							}
						}
					} else if (bss.getEncryption() == BSS.WPA) {
						if (wpaPairsDB.contains(bss.getId())) {
							Vector<String> v = wpaPairsDB.get(bss.getId());
							bss.setWPAPair(v.elementAt(0), v.elementAt(1));
						} else {
							BufferedReader br = new BufferedReader(
									new InputStreamReader(System.in));
							System.out.println("Insert WPA username:");
							String newWPAUsername = br.readLine();
							System.out.println("Insert WPA password:");
							String newWPAPassword = br.readLine();
							if (!newWPAUsername.equals("")
									&& !newWPAPassword.equals("")) {
								bss.setWPAPair(newWPAUsername, newWPAPassword);
								Vector<String> v = new Vector<String>();
								v.addElement(newWPAUsername);
								v.addElement(newWPAPassword);
								wpaPairsDB.put(bss.getId(), v);
							}
						}
					}// fine registrazione sicurezza.

					IEEE80211 ieee80211 = (IEEE80211) connector
							.getLocalNetworkInterface();

					String newIPAddress = ieee80211.connect(bss);

					if (newIPAddress != null)
						newSuitableChannels.addElement(new Channel(connector,
								newIPAddress));
				} else if (connector.getConnectorDescription() instanceof RemoteBluetooth) {
					RemoteBluetooth remoteBluetooth = (RemoteBluetooth) connector
							.getConnectorDescription();
					Bluetooth bluetooth = (Bluetooth) connector
							.getLocalNetworkInterface();

					String newIPAddress = bluetooth.connect(remoteBluetooth);

					if (newIPAddress != null)
						newSuitableChannels.addElement(new Channel(connector,
								newIPAddress));
				} else {
					if (connector.getConnectorDescription() instanceof LocationOss) {
						System.out.println("\tLocationOss!!!!!!!!!!!! ");
					}
					throw new Exception("Unknown connector type: "
							+ connector.getConnectorDescription());
				}
			}
		}

		// riaggiungo canali gia` precedentemente connessi
		/*
		 * for(Channel c : suitableChannels){
		 * if(!newSuitableChannels.contains(c)) newSuitableChannels.add(c); }
		 */
		newSuitableChannels.addAll(suitableChannels);

		// System.out.println("ConnectorManager newSuitableChannels "+newSuitableChannels);

		for (int i = 0; i < newSuitableChannels.size(); i++) {
			Channel availableChannel = newSuitableChannels.elementAt(i);
			if (availableChannel.getConnector().getConnectorDescription() == null) {
				// si saltano le interfacce senza connector description
				// (Location), cioe` fondamentalmente Ethernet
				continue;
			}
			String availableChannelMAC = availableChannel.getConnector()
					.getConnectorDescription().getId();
			boolean toDeleteChannel = true;
			for (int j = 0; j < suitableConnectors.size()
					&& toDeleteChannel == true; j++) {
				Connector aConnector = suitableConnectors.elementAt(j);
				if (availableChannelMAC.equals(aConnector
						.getConnectorDescription().getId())) {
					toDeleteChannel = false;
				}
			}
			if (toDeleteChannel) {
				newSuitableChannels.removeElementAt(i);
				i--;
			}
		}

		return newSuitableChannels;
	}
}
